﻿@using Gurux.DLMS.AMI.Shared.DIs
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.Rest
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Client.Helpers
@using Gurux.DLMS.AMI.Module
@using Gurux.DLMS.AMI.Client.Pages.Device
@using System.IO.Ports;

<div class="row">
    <div style="width:100%">
        <div class="col-auto">
            <label>@Properties.Resources.Baudrate</label>
            <select class="form-select" @bind="BaudRate">
                @foreach (var it in BaudRateList)
                {
                    <option value="@it">@it</option>
                }
            </select>
        </div>
        <div class="col-auto">
            <label>@Properties.Resources.DataBits</label>
            <select class="form-select" @bind="DataBits">
                @foreach (var it in DataBitsList)
                {
                    <option value="@it">@it</option>
                }
            </select>
        </div>
        <div class="col-auto">
            <label>@Properties.Resources.Parity</label>
            <select class="form-select" @bind="Parity">
                @foreach (var it in ParityList)
                {
                    <option value="@it">@it</option>
                }
            </select>
        </div>
        <div class="form-group">
            <label>@Properties.Resources.StopBits</label>
            <select class="form-select" @bind="StopBits">
                @foreach (var it in StopBitList)
                {
                    <option value="@it">@it</option>
                }
            </select>
        </div>
    </div>
</div>
@code {
    private string? _settings;

    [Parameter]
    public string? Settings
    {
        get => _settings;
        set
        {
            if (_settings != value)
            {
                _settings = value;
                SettingsChanged.InvokeAsync(value);
            }
        }
    }

    /// <summary>
    /// Notified when settings are updated.
    /// </summary>
    [Parameter]
    public EventCallback<string?> SettingsChanged { get; set; }

    /// <summary>
    /// Available baud rates.
    /// </summary>
    private int[] BaudRateList = new[] { 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 38400, 56000, 57600, 115200, 128000 };

    /// <summary>
    /// Available parities.
    /// </summary>
    private Parity[] ParityList = new[] { Parity.None, Parity.Odd, Parity.Even, Parity.Mark, Parity.Space };

    /// <summary>
    /// Available stop bits.
    /// </summary>
    private StopBits[] StopBitList = new[] { StopBits.None, StopBits.One, StopBits.Two };

    /// <summary>
    /// Available data bits.
    /// </summary>
    private int[] DataBitsList = new[] { 7, 8 };

    private GXSerialTemplate _serial = new GXSerialTemplate();

    private int BaudRate
    {
        get
        {
            if (_serial == null)
            {
                return 9600;
            }
            return _serial.BaudRate;
        }
        set
        {
            _serial.BaudRate = value;
            Settings = _serial.Settings;
        }
    }

    private int DataBits
    {
        get
        {
            if (_serial == null)
            {
                return 8;
            }
            return _serial.DataBits;
        }
        set
        {
            _serial.DataBits = value;
            Settings = _serial.Settings;
        }
    }

    private Parity Parity
    {
        get
        {
            if (_serial == null)
            {
                return Parity.None;
            }
            return (Parity)_serial.Parity;
        }
        set
        {
            _serial.Parity = (int)value;
            Settings = _serial.Settings;
        }
    }

    private StopBits StopBits
    {
        get
        {
            if (_serial == null)
            {
                return StopBits.None;
            }
            return (StopBits)_serial.StopBits;
        }
        set
        {
            _serial.StopBits = (int)value;
            Settings = _serial.Settings;
        }
    }

    protected override void OnInitialized()
    {
        try
        {
            _serial.Settings = _settings;
        }
        catch (Exception)
        {
            //Settings might fail when media type is changed.
        }
    }

    /// <summary>
    /// Validate serial port settings.
    /// </summary>
    public void Validate(GXValidator sender)
    {

    }
}
